package gui;

import protocol.generic.AbstractProtocolManager;
import protocol.generic.Group;

import com.trolltech.qt.core.QObject;
import com.trolltech.qt.gui.QCloseEvent;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QMessageBox;
import com.trolltech.qt.gui.QWidget;

public class AddGroup extends QDialog{
	MainWindow mainwindow;
	Ui_AddGroup ui_addgroup=new Ui_AddGroup();
	
	public AddGroup(){
		super();
	}
	
	public AddGroup(MainWindow mw){
		super(mw);
		this.mainwindow=mw;
		
		ui_addgroup.setupUi(this);
		ui_addgroup.cancelButton.clicked.connect(this, "close()");
		ui_addgroup.OKButton.clicked.connect(this, "close()");
		ui_addgroup.OKButton.clicked.connect(this, "addGroupToList()");
		this.show();
	}
	public void addGroupToList(){
		String groupName=this.ui_addgroup.groupEdit.text();
		for(AbstractProtocolManager ma : mainwindow.protocolManagers){
			for(Group group : ma.getGroups()){
				if(group.getName().equalsIgnoreCase(groupName)){
					QMessageBox.critical(this, "Group add error", "The group already exists here");
					return;
				}
			}
		}
		
		this.mainwindow.buddyList.groupList.add(new Group(groupName));
		this.mainwindow.buddyList.layoutChanged.emit();
		
	}
}
